<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * 
 */
class Users_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function fetchUsers() {
        $query = $this->db->get('users');
        return $query->result_array();
    }

    // START OF HASH...

    public function updateHash($hash, $email) {
        $sql = "UPDATE users SET forgotten_pw_hash='$hash' WHERE email='$email'";
        $this->db->query($sql);
    }

    public function performUpdateAreas($areas, $user_id) {
        $sql = "UPDATE users SET areas='$areas' WHERE user_id='$user_id'";
        if ($this->db->query($sql))
            echo 'ok';
    }

    public function performUpdateKeywords($keywords, $user_id) {
        $sql = "UPDATE users SET keywords='$keywords' WHERE user_id='$user_id'";
        if ($this->db->query($sql))
            echo 'ok';
    }

    public function checkUserAreas($user_id) {
        $sql = "SELECT areas from users WHERE user_id='$user_id'";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

    public function checkUserKeywords($user_id) {
        $sql = "SELECT keywords from users WHERE user_id='$user_id'";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

    public function checkHash($hash) {
        $sql = "SELECT * FROM users WHERE forgotten_pw_hash='$hash'";
        $res = $this->db->query($sql);
        if ($res->num_rows() == 1) {
            $pole = $res->row_array();

            $heslo = substr(md5(rand(1, 50000)), 0, 6);
            $heslo_crypt = md5($heslo);
            $sql2 = "UPDATE users SET forgotten_pw_hash=NULL,password='$heslo_crypt' WHERE forgotten_pw_hash='$hash'";
            $this->db->query($sql2);

            return array('pole' => $pole,
                'heslo' => $heslo);
        }
    }

    // END OF HASH FUNC>...

    public function fetchLastUser() {
        $this->db->order_by("user_id", "desc");
        $query = $this->db->get('users', 1);
        return $query->row_array();
    }

    public function fetchUsersCount() {
        return $this->db->count_all('users');
    }

    public function insertUser($login, $password, $email) {
        # code...
    }

    public function fetchUserReportsCount($user_id) {
        $sql = "SELECT n_police,n_patrol,n_accident,n_mobileradar from users where user_id='$user_id'";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

    public function plusOneReport($id, $what) {
        $sql = "UPDATE users SET $what=$what+1 WHERE user_id='$id'";
        $this->db->query($sql);
    }

    public function fetchUserReportsCountByLogin($login) {
        $sql = "SELECT n_police,n_patrol,n_accident,n_mobileradar,count(comment_id) as comments from users join comments using (user_id) where login='$login'";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

}

?>